# import xlrd
# import csv

# def xlsx_to_csv():
#     workbook = xlrd.open_workbook(r'F:\farm2client\config\config_xls\t_base_crop.xlsx')
#     table = workbook.sheet_by_index(0)
#     with codecs.open('t_base_crop.csv', 'w', encoding='utf-8') as f:
#         write = csv.writer(f)
#         for row_num in range(table.nrows):
#             row_value = table.row_values(row_num)
#             write.writerow(row_value)

# if __name__ == '__main__':
#     xlsx_to_csv()
import pandas as pd
import os
import sys
from os import path
import sys
def xlsx_to_csv_pd():
    d = path.dirname(__file__)  #返回当前文件所在的目录
    print("路径="+d)
    xlsfile = r'F:\farm2client\config\config_xls\t_base_crop.xlsx'
    data_xls = pd.read_excel(xlsfile, index_col=0)
    data_xls.to_csv(r'F:\farm2client\config\config_xls\t_base_crop.csv', encoding='utf-8')


#读取目录所有excel文件
def read_path(path):
    dirs =  os.listdir(path)
    return dirs
    
#建立单个文件的excel转换成csv函数,file 是excel文件名，to_file 是csv文件名。
def excel_to_csv(file,to_file):
    data_xls=pd.read_excel(file, index_col=0)
    data_xls.to_csv(to_file,encoding='utf_8',float_format='%.1f')
 

def main():
    #源文件路径
    xlsPath=os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    source = xlsPath+"\\config_xls"
    #目标文件路径
    ob=xlsPath+"\\config_csv"
    dirs=read_path(source)
    file_list = [source+'\\'+i for i in dirs]
    j=1
    #建立循环对每个文件调用excel_to_csv()
    for it in file_list:
        #j_mid=str(j)
        arr=dirs[j-1].split(".")
        file_name=arr[0]
        j_csv=ob+'\\'+file_name+".csv"
        excel_to_csv(it,j_csv)
        print(it)
        j=j+1
d=""
if __name__ == '__main__':
    # d = path.dirname(__file__)  #返回当前文件所在的目录
    # a=sys.path[0]
    # print("d="+d)
    # print("a="+a)

    # c=os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    # print("c"+c)
    # source=c+"\\config_xls"
    # #source = "F:\\farm2client\\config\\config_xls"
    # dirs=read_path(source)
    # print("c"+c)
    main()
# if __name__ == '__main__':
#     xlsx_to_csv_pd()
